/**
 * 
 */
package com.agilegroup.crm.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import com.agilegroup.crm.AgileApplication;
import com.agilegroup.crm.adapters.ViewDetailsAdapter;
import com.agilegroup.crm.model.Call;
import com.agilegroup.crm.model.EntryItem;
import com.agilegroup.crm.model.Item;
import com.agilegroup.crm.model.SectionItem;
import com.agilegroup.crm.requests.ViewCallJsonRequest;
import com.agilegroup.crm.utils.SessionManager;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.SpiceException;
import com.octo.android.robospice.request.listener.RequestListener;

/**
 * @author Hisham
 *
 */
public class ViewCallActivity extends SherlockListActivity {
    /** Called when the activity is first created. */
	
	private SessionManager sessionManager;
	private ArrayList<Item> items = new ArrayList<Item>();
	private static final String JSON_CACHE_KEY = "view_call_json";
	
	@SuppressWarnings("static-access")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        String callId = intent.getStringExtra("callId");
        Log.d("callId", callId);
        sessionManager = new SessionManager(getBaseContext());    
    	HashMap<String, String> user = sessionManager.getUserDetails();
    	String empId = user.get(sessionManager.KEY_ID);
        ArrayList<String>parameters = new ArrayList<String>();
        parameters.add(callId);
        parameters.add(empId);
        AgileApplication.getInstance().spiceManager.execute(new ViewCallJsonRequest(parameters),JSON_CACHE_KEY,DurationInMillis.ALWAYS_EXPIRED,new ViewCallJsonRequestListener());
    }
    
    @SuppressLint("SimpleDateFormat")
	public class ViewCallJsonRequestListener implements RequestListener<Call>{

		public void onRequestFailure(SpiceException spiceException) {
			Toast.makeText(ViewCallActivity.this, "Failure", Toast.LENGTH_SHORT).show();
		}
		public void onRequestSuccess(Call call) {
			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			items.add(new SectionItem("Call Information"));
			if(call.getOwnerName()!=null&&!call.getOwnerName().equalsIgnoreCase("")){
				items.add(new EntryItem("Call Owner", call.getOwnerName()));
			}
			else{
				items.add(new EntryItem("Call Owner", "  "));
			}
			if(call.getCrmStaffName()!=null&&!call.getCrmStaffName().equalsIgnoreCase("")){
				items.add(new EntryItem("CRM Staff", call.getCrmStaffName()));
			}
			else{
				items.add(new EntryItem("CRM Staff", "  "));
			}
			if(call.getContactedPersonName()!=null&&!call.getContactedPersonName().equalsIgnoreCase("")){
				items.add(new EntryItem("Contacted Person", call.getContactedPersonName()));
			}
			else{
				items.add(new EntryItem("Contacted Person", "   "));
			}
			if(call.getDate()!=null){
				String formattedDate = formatter.format(call.getDate());
				items.add(new EntryItem("Date", formattedDate));
			}
			else{
				items.add(new EntryItem("Date", "   "));
			}
			if(call.getStartTime()!=null&&!call.getStartTime().equalsIgnoreCase("")){
				items.add(new EntryItem("Starting Time", call.getStartTime()));
			}
			else{
				items.add(new EntryItem("Starting Time", "   "));
			}
			if(call.getEndTime()!=null&&!call.getEndTime().equalsIgnoreCase("")){
				items.add(new EntryItem("Ending Time", call.getEndTime()));
			}
			else{
				items.add(new EntryItem("Ending Time", "   "));
			}
	       
			if(call.getInOutCall()!=null){
				if(call.getInOutCall()==true){
					items.add(new EntryItem("In/Out Call", "In"));
				}
				else{
					items.add(new EntryItem("In/Out Call", "Out"));
				}
			}
			else{
				items.add(new EntryItem("In/Out Call", "   "));
			}
			
			if(call.getDecisionMaker()!=null){
				if(call.getDecisionMaker()==true){
					items.add(new EntryItem("Decision Maker", "Yes"));
				}
				else{
					items.add(new EntryItem("Decision Maker", "No"));
				}
			}
			else{
				items.add(new EntryItem("Decision Maker", "   "));
			}
			
			if(call.getComment()!=null&&!call.getComment().equalsIgnoreCase("")){
				 items.add(new EntryItem("Comment", call.getComment()));
			}
			else{
				items.add(new EntryItem("Comment", "   "));
			}
			
	        items.add(new SectionItem("Next Follow Up Information"));
	        if(call.getNextFollowUp()!=null){
	        	if(call.getNextFollowUp()==true){
	        		items.add(new EntryItem("Next Follow Up", "Yes"));
	        	}
	        	else{
	        		items.add(new EntryItem("Next Follow Up", "No"));
	        	}
	        }
	        else{
				items.add(new EntryItem("Next Follow Up", "   "));
			}	
	        
	        if(call.getFollowUpType()!=null&&!call.getFollowUpType().equalsIgnoreCase("")){
	        	if(call.getFollowUpType().equals("appointment")){
	        		items.add(new EntryItem("Next Follow Up Type", "Appointment"));
	        	}
	        	else{
	        		if(call.getFollowUpType().equals("none")){
	        			items.add(new EntryItem("Next Follow Up Type", "   "));
	        		}
	        		else{
	        			items.add(new EntryItem("Next Follow Up Type", "Call"));
	        		}
	        	}
	        }
	        else{
				items.add(new EntryItem("Next Follow Up Type", "   "));
			}
	        
	        if(call.getFollowUpDate()!=null){
	        	String formattedDate = formatter.format(call.getFollowUpDate());
	        	items.add(new EntryItem("Next Follow Up Date", formattedDate));
	        }
	        else{
				items.add(new EntryItem("Next Follow Up Date", "   "));
			}
	        if(call.getFollowUpDetails()!=null&&!call.getFollowUpDetails().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Next Follow Up Details", call.getFollowUpDetails()));
	        }
	        else{
				items.add(new EntryItem("Next Follow Up Details", "   "));
			}
	        ViewDetailsAdapter adapter = new ViewDetailsAdapter(ViewCallActivity.this, items);
	        setListAdapter(adapter);
		}
	 }
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	int itemId = item.getItemId();
        switch (itemId) {
        case android.R.id.home:
             finish();
            break;

        }

        return true;
    }
}
